import { computed, reactive } from 'vue'
import Utils from '@/utils'
import TargetClient from '@/api/targetClient.js'
import store from '@/store/index.js'
const briefInfo = computed(() => store.state.client.briefInfo)
export function useRules(clientId, apiData, dict) {
  const rules = {
    fullName: [
      {
        required: true,
        whitespace: true,
        message: 'Please input Client Name',
        trigger: 'blur'
      }
    ],
    countryID: [
      {
        required: true,
        type: 'number',
        min: 1,
        message: 'Please select',
        trigger: 'blur'
      }
    ],
    parentID: [
      {
        required: true,
        type: 'number',
        message: 'Please select',
        trigger: 'blur'
      },
      {
        validator: (rule, value) => {
          return value !== 0 ? Promise.resolve() : Promise.reject(new Error())
        },
        message: 'Please select',
        trigger: 'blur'
      }
    ],
    timeZone: [
      {
        required: true,
        message: 'Please select a time zone',
        trigger: 'blur'
      }
    ],
    clientKind: [
      {
        required: true,
        type: 'number',
        message: 'Please select',
        trigger: 'blur'
      }
    ],
    businessDirection: [
      {
        required: true,
        type: 'number',
        message: 'Please select',
        trigger: 'blur'
      }
    ],
    clientTier: [
      {
        required: true,
        type: 'number',
        min: 1,
        message: 'Please select',
        trigger: 'blur'
      }
    ],
    email: [
      {
        required: true,
        whitespace: true,
        message: 'Please input email',
        trigger: 'blur'
      },
      {
        type: 'email',
        message: 'Incorrect email address format',
        trigger: 'blur'
      }
    ],
    shortName: [
      {
        required: true,
        whitespace: true,
        message: 'Please input Abbreviation',
        trigger: 'blur'
      },
      {
        asyncValidator: (rule, value) => TargetClient.isUniqueAbbreviation(rule, value, clientId),
        message: 'Abbreviation is exist',
        trigger: 'blur'
      }
    ],
    url: [
      {
        required: true,
        whitespace: true,
        message: 'Please input url',
        trigger: 'blur'
      },
      {
        validator: (rule, value) => {
          const regex = Utils.getRegex('url')
          const urls = value.split(';').filter(url => url.trim() !== '')
          if (urls.length > 0 && urls.length === value.split(';').length && urls.every(url => regex.test(url.trim()))) {
            return Promise.resolve()
          } else {
            return Promise.reject(new Error())
          }
        },
        message:
          'Incorrect url address format. eg. https://google.com, If there are multiple URLs, separate them by semicolons.',
        trigger: 'blur'
      },
      {
        asyncValidator: (rule, value) => TargetClient.isUniqueUrl(rule, value, clientId, apiData.parentID),
        message: 'This URL already exists in another organization.',
        trigger: 'blur'
      }
    ]
  }
  if (!briefInfo.value.isExisting) {
    rules.saleBUID = [
      {
        required: true,
        type: 'number',
        min: 1,
        message: 'Please select',
        trigger: 'blur'
      }
    ]
    rules.hrSalesBUID = [
      {
        // 如果没有MyBU可选则不必填
        required: computed(() => dict.value.ownersBUs.length > 0).value,
        type: 'number',
        min: computed(() => dict.value.ownersBUs.length > 0).value ? 1 : 0,
        message: 'Please select',
        trigger: 'blur'
      }
    ]
  }
  if (briefInfo.value.phaseID === 1 || briefInfo.value.isExisting) {
    rules.city = [
      {
        required: true,
        whitespace: true,
        message: 'Please Input City/State',
        trigger: 'blur'
      }
    ]
    rules.postalCode = [
      {
        required: true,
        whitespace: true,
        message: 'Please Input Zip Code',
        trigger: 'blur'
      }
    ]
    rules.address = [
      {
        required: true,
        whitespace: true,
        message: 'Please Input Address',
        trigger: 'blur'
      }
    ]
  }
  return reactive(rules)
}
